class Promise {
      callbacks = [];
      state = 'pending';//增加状态
      value = null;//保存结果
      constructor(fn) {
          fn(this._resolve.bind(this), this._reject.bind(this));
      }
      then(onFulfilled, onRejected) {
          return new Promise((resolve, reject) => {
              this._handle({
                  onFulfilled: onFulfilled || null,
                  onRejected: onRejected || null,
                  resolve: resolve,
                  reject: reject
              });
          });
      }
      _handle(callback) {
          if (this.state === 'pending') {
              this.callbacks.push(callback);
              return;
          }
   
          let cb = this.state === 'fulfilled' ? callback.onFulfilled : callback.onRejected;
   
          if (!cb) {//如果then中没有传递任何东西
              cb = this.state === 'fulfilled' ? callback.resolve : callback.reject;
              cb(this.value);
              return;
          }
   
          let ret = cb(this.value);
          cb = this.state === 'fulfilled' ? callback.resolve : callback.reject;
          cb(ret);
      }
      _resolve(value) {
   
          if (value && (typeof value === 'object' || typeof value === 'function')) {
              var then = value.then;
              if (typeof then === 'function') {
                  then.call(value, this._resolve.bind(this), this._reject.bind(this));
                  return;
              }
          }
   
          this.state = 'fulfilled';//改变状态
          this.value = value;//保存结果
          this.callbacks.forEach(callback => this._handle(callback));
      }
      _reject(error) {
          this.state = 'rejected';
          this.value = error;
          this.callbacks.forEach(callback => this._handle(callback));
      }
  }
  //Promise.resolve
  Promise.resolve=function(value){
      if (value && value instanceof Promise) {
        return value;
      } else if (value && typeof value === 'object' && typeof value.then === 'function') {
        let then = value.then;
        return new Promise(resolve => {
          then(resolve);
        });
      } else if (value) {
        return new Promise(resolve => resolve(value));
      } else {
        return new Promise(resolve => resolve());
      }
    }
    //Promise.reject
    Promise.reject = function(reason) {
          return new Promise((resolve, reject) => reject(reason))
      }